package com.myproject.security.simpledemo.service;

import com.myproject.security.simpledemo.entity.Role;
import com.myproject.security.simpledemo.entity.User;
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (!("user").equals(username) && !("admin").equals(username)) {
            throw new UsernameNotFoundException("账户不存在!");
        }
        User user = new User();
        if(("user").equals(username)){
            user.setEnabled(true);
            user.setId(1);
            user.setLocked(false);
            user.setPassword("123456");
            user.setUsername("user");
            List<Role> roles = new ArrayList<>();
            Role role = new Role();
            role.setId(2);
            role.setName("ROLE_USER");
            role.setNameZh("普通用户");
            roles.add(role);
            user.setRoles(roles);
        }else{
            user.setEnabled(true);
            user.setId(1);
            user.setLocked(false);
            user.setPassword("123456");
            user.setUsername("admin");
            List<Role> roles = new ArrayList<>();
            Role role = new Role();
            role.setId(1);
            role.setName("ROLE_ADMIN");
            role.setNameZh("管理员");
            roles.add(role);
            role = new Role();
            role.setId(2);
            role.setName("ROLE_USER");
            role.setNameZh("普通用户");
            roles.add(role);
            user.setRoles(roles);
        }
        return user;
    }
}